package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.project.system.domain.SysType;
import com.ruoyi.project.system.service.ISysTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysAttribute;
import com.ruoyi.project.system.service.ISysAttributeService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 分类属性Controller
 *
 * @author jjp
 * @date 2020-04-20
 */
@RestController
@Api(description = "分类属性", tags = "分类属性")
@RequestMapping("/system/attribute")
public class SysAttributeController extends BaseController {
    @Autowired
    private ISysAttributeService sysAttributeService;
    @Autowired
    private ISysTypeService sysTypeService;

    /**
     * 查询分类属性列表
     */
    @PreAuthorize("@ss.hasPermi('system:attribute:list')")
    @ApiOperation("查询分类属性列表")
    @GetMapping("/list")
    public TableDataInfo list(SysAttribute sysAttribute) {
        startPage();
        List<SysAttribute> list = sysAttributeService.selectSysAttributeList(sysAttribute);
        return getDataTable(list);
    }

    /**
     * 获取分类属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:attribute:query')")
    @ApiOperation("获取分类属性详细信息")
    @GetMapping(value = "/{attributeId}")
    public AjaxResult getInfo(@PathVariable("attributeId") Long attributeId) {
        return AjaxResult.success(sysAttributeService.selectSysAttributeById(attributeId));
    }

    /**
     * 新增分类属性
     */
    @ApiOperation("新增分类属性")
    @PreAuthorize("@ss.hasPermi('system:attribute:add')")
    @Log(title = "分类属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysAttribute sysAttribute) {
        sysAttributeService.insertSysAttribute(sysAttribute);
        return AjaxResult.success();
    }

    /**
     * 修改分类属性
     */
    @PreAuthorize("@ss.hasPermi('system:attribute:edit')")
    @ApiOperation("修改分类属性")
    @Log(title = "分类属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAttribute sysAttribute) {
        return toAjax(sysAttributeService.updateSysAttribute(sysAttribute));
    }

    /**
     * 删除分类属性
     */
    @PreAuthorize("@ss.hasPermi('system:attribute:remove')")
    @ApiOperation("删除分类属性")
    @Log(title = "分类属性", businessType = BusinessType.DELETE)
    @DeleteMapping("/{attributeIds}")
    public AjaxResult remove(@PathVariable Long[] attributeIds) {
        return toAjax(sysAttributeService.deleteSysAttributeByIds(attributeIds));
    }


    /**
     * 获取分类属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:attribute:query')")
    @ApiOperation("根据typeId获取分类属性详细信息")
    @GetMapping(value = "/getInfoByTypeId/{typeId}")
    public AjaxResult getInfoByTypeId(@PathVariable("typeId") Long typeId) {
        List<SysAttribute> sysAttributes = sysAttributeService.selectSysAttributeByTypeId(typeId);
        return AjaxResult.success(sysAttributes);
    }
}
